﻿using cSharpStudents415.Data;
using cSharpStudents415.Data.Models;
using cSharpStudents415.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace cSharpStudents415.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            using (StudentsDbContext db = new StudentsDbContext())
            {
                //Create a VM
                StudentsIndexVm sVm = new StudentsIndexVm();
                //Populate Vm
                sVm.Greeting = "Hello Hackathon !!!";
                sVm.MyAlumniList = db.Alumni.ToList();
                //Send Vm to the view
                return View(sVm);
            }
            
        }

        public ActionResult Details(int sid)
        {
            using (StudentsDbContext db = new StudentsDbContext())
            {
                //Create a VM
                StudentDetailsVm dVm = new StudentDetailsVm();
                //Populate a VM
                dVm.Greeting = "Hello from Student Details...";
                dVm.StudentDetails = db.Alumni
                                     .FirstOrDefault(c => c.AlumniId == sid);
               // dVm.CourseDetails = db.Alumni.Include("Courses").ToList();
                
               dVm.CourseDetails = db.Courses.FirstOrDefault(c => c.CourseId == dVm.StudentDetails.CourseId);
                //Send VM data to the view
                return View(dVm);
            }
            
        }
  //
        public ActionResult Edit(int sid) 
        {
            using (StudentsDbContext db = new StudentsDbContext()) 
            {
                //create a vm
                StudentsEditVm eVm = new StudentsEditVm();
                //populate
                eVm.Greeting = "hello, im here to edit";
                eVm.AlumniDetails = db.Alumni.FirstOrDefault(c => c.AlumniId == sid);

                //send the vm
                return View(eVm);
            }
        }
        [HttpPost]
        public ActionResult Edit(StudentsEditVm SaveVm) 
        {
            using (StudentsDbContext db = new StudentsDbContext()) 
            {
                Alumni tempAlumni        = db.Alumni.FirstOrDefault(c => c.AlumniId == SaveVm.AlumniDetails.AlumniId);
                tempAlumni.EmailAddress     = SaveVm.AlumniDetails.EmailAddress;
                tempAlumni.CourseId         = SaveVm.AlumniDetails.CourseId;
                tempAlumni.GradeAverage     = SaveVm.AlumniDetails.GradeAverage;
                tempAlumni.Horoscope        = SaveVm.AlumniDetails.Horoscope;
                tempAlumni.Level            = SaveVm.AlumniDetails.Level;
                tempAlumni.Name             = SaveVm.AlumniDetails.Name;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}